/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hacker
 */
@WebServlet(name="productoscliente", urlPatterns={"/productoscliente"})
public class productoscliente extends HttpServlet {
   Connection c;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException, ClassNotFoundException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
//                 c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tablas", "root", "root");
//            Statement sql = (Statement) c.createStatement();
//
//              ResultSet datos = sql.executeQuery("select * from productos ");
//
//
//
//            out.println("<html>");
//            out.println("<head>");
//            out.println("<title>Servlet Registrarse</title>");
//            out.println("</head>");
//            out.println("<body bgcolor='#66CCFF'>");
//            out.println("<table border ='2'width='400' align='center'>");
//            out.println(" <tr style='color:#FFF'>" +
//                    "<th bgcolor='#000099' scope='col'>id</th>" +
//                    "<th bgcolor='#000099' scope='col'>nombre</th> " +
//                    "<th bgcolor='#000099' scope='col'>marca</th>" +
//                    "<th bgcolor='#000099' scope='col'>precio</th> " +
//                    "</tr>");
//
//
//      while(datos.next()){
//           out.println("<tr align='center'><td>"+datos.getString("id")+"" +
//                   "" +
//                   "</td><td>"+datos.getString("nombre")+"</td>" +
//                   "<td>"+datos.getString("marca")+"</td>" +
//                   "<td>"+datos.getString("precio")+"</td>" +
//                  // "<td>"+datos.getString("precio")+"</td>" +
//                   "</tr>");
//                       }
//    //       out.println("<a href='perfiles'>Ir a tabla mi perfil</a>");
//           out.println("</table>");
//
//            out.println("</body>");
//            out.println("</html>");

            ///////////////////////////////////////////////////////////////////////////////////////////

             String fil=request.getParameter("buscar");

      //      HttpSession sesion=request.getSession(false);
//
//            if(sesion== null){
//            response.sendRedirect("login");
//            }else{


      if (fil==null)
          fil="";
             Class.forName("org.gjt.mm.mysql.Driver");
                 c = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tablas", "root", "root");
            Statement sql = (Statement) c.createStatement();

              ResultSet datos = sql.executeQuery("select * from productos where nombre like '%"+fil+"%' ");



            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Registrarse</title>");
            out.println("</head>");
            out.println("<body background='Imagenes/oso corazon.gif'>");

//                out.println("<table border ='2'width='400' align='center'>");
//                out.println(" <tr style='color:#FFF'>" +
//
//                    "</tr>");

            out.println("<font Size='10' color='Red'>");
            out.println("<MARQUEE> MOTIVOS AGRADECE SU PREFERENCIA </MARQUEE>");
            out.println("<br/>");
            out.println("</font>");
               out.println("<br>");




out.println("<MARQUEE><img src='./Imagenes/m1.gif'/><img src='./Imagenes/m2.jpg'/><img src='./Imagenes/m3.jpg'/><img src='./Imagenes/m4.jpg'/><img src='./Imagenes/m5.jpg'/></MARQUEE>");





                out.println("</table>");
                out.println("<center>");

              out.println("<form id='form1' name='filtro' method='get' action='productoscliente'>");
              out.println(" <p><label>Producto: <input type='text' name='buscar' id='textfield1'/> <input type='submit' name='button' id='button' value='buscar' /> </label> </p>");
              out.println("</form>");

 out.println("<br>");
            
            
            out.println("<a href='productoscliente'>Productos</a>");
            out.println("<br>");
 out.println("<a href='login'>Salir</a>");

       out.println("</center>");


            out.println("<table bgcolor='#DB7093' border ='2'width='400' align='center'>");
            out.println(" <tr style='color:#FFF'>" +
                    "<th bgcolor='#000099' scope='col'>id</th>" +
                    "<th bgcolor='#000099' scope='col'>nombre</th> " +
                    "<th bgcolor='#000099' scope='col'>marca</th>" +
                    "<th bgcolor='#000099' scope='col'>precio</th> " +
                    "</tr>");


      while(datos.next()){
           out.println("<tr align='center'><td>"+datos.getString("id")+"" +
                   "" +
                   "</td bgcolor=''><td>"+datos.getString("nombre")+"</td>" +
                   "<td>"+datos.getString("marca")+"</td>" +
                   "<td>"+datos.getString("precio")+"</td>" +
                  // "<td>"+datos.getString("precio")+"</td>" +
                   "<td><input type='button' value='Comprar' onclick= \"if(confirm('¿Deseas comprar este articulo?')){location.href='eliminarproducto1?ids="+datos.getString("id")+"'} \"/>"+


                   "</tr>");
                       }
// out.println("<a href='Mostrar'>Ir a tabla de usuarios</a>");
 out.println("<br>");
 out.println("<a href='login'>Salir</a>");
 out.println("<br>");
//  out.println("<a href='perfiles'>Ir a tabla mi perfil</a>");
           out.println("</table>");

            out.println("</body>");
            out.println("</html>");


        } finally {
            out.close();
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(productoscliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mostrarusuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            try {
                processRequest(request, response);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(productoscliente.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (SQLException ex) {
            Logger.getLogger(mostrarusuarios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
